package br.com.virtual.vender.dao.impl;

import java.util.ArrayList;
import java.util.List;

import br.com.virtual.base.dao.impl.BaseDaoImpl;
import br.com.virtual.vender.dao.InteresseDAO;
import br.com.virtual.vender.model.Interesse;
import br.com.virtual.vender.model.ItemVenda;

public class InteresseDAOImpl extends BaseDaoImpl implements InteresseDAO {

	@Override
	public Interesse create(Interesse interesse) {

		getManager().persist(interesse);
		return interesse;
	}

	@Override
	public Interesse read(Long id) {
		return getManager().find(Interesse.class, id);
	}

	@Override
	public Interesse update(Interesse interesse) {
		return getManager().merge(interesse);
	}

	@Override
	public void delete(Interesse interesse) {
		getManager().remove(read(interesse.getId()));
	}

	@Override
	public List<ItemVenda> getItens(Interesse interesse) {

		List<ItemVenda> iv = getManager()
				.createQuery(
						" select iv from ItemVenda iv where iv.interesse = :interesse",
						ItemVenda.class).setParameter("interesse", interesse)
				.getResultList();

		return new ArrayList<ItemVenda>(iv);
	}
}